/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package desk;

import com.itextpdf.text.DocumentException;
import ejb.*;
import helpClasses.ReportProperties;
import hibernate.*;
import java.io.IOException;
import java.util.*;
import java.util.logging.*;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import org.primefaces.model.DualListModel;

/**
 * Бин вывода отчетов
 * @author AGolubinskyy
 */
@ManagedBean
public class ReporterBean {
    @ManagedProperty(value = "#{webClientController}")
    private WebClientController clientController;
    private Security security;
    private RequestManager requestManager;
    private ProductManager productManager;
    private ReportManager reportManager;
    private DualListModel<User> users;
    private DualListModel<User> managers;
    private DualListModel<RequestStatus> statuses;
    private DualListModel<Product> products;
    private DualListModel<ProductCategory> categories;
    private Date dateFrom;
    private Date dateTo;
    private String description, message;
    private int common;
    
    /**
     * Получает списки для отображения на странице
     */
    @PostConstruct
    public void init(){
        users = new DualListModel<User>(security.getAllUsers(), new ArrayList<User>());
        managers = new DualListModel<User>(security.getUsersByRole(UserRole.GET_MANAGER_ROLE()), new ArrayList<User>());
        statuses = new DualListModel<RequestStatus>(requestManager.getAllRequestStatuses(), new ArrayList<RequestStatus>());
        products = new DualListModel<Product>(productManager.getAllProducts(), new ArrayList<Product>());
        categories = new DualListModel<ProductCategory>(productManager.getAllCategories(), new ArrayList<ProductCategory>());
    }

    /**
     * Выводит файл в поток на основе введенных данных
     */
    public void exportPdfFile() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        response.setContentType("application/pdf");
        response.setHeader("Content-disposition", "inline=filename=report.pdf");

        try {           
            PdfCreator p = new PdfCreator("template.properties");
            p.createPdfSaveToStream(getRequests(), response.getOutputStream(), false);
            response.getOutputStream().flush();
            response.getOutputStream().close();
            context.responseComplete();
        } catch (DocumentException ex) {
            Logger.getLogger(RquestListerBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException e) {
        }
    }
    public DualListModel<User> getUsers() {
        return users;
    }

    public void setUsers(DualListModel<User> users) {
        this.users = users;
    }

    public DualListModel<User> getManagers() {
        return managers;
    }

    public void setManagers(DualListModel<User> managers) {
        this.managers = managers;
    }

    public DualListModel<RequestStatus> getStatuses() {
        return statuses;
    }

    public void setStatuses(DualListModel<RequestStatus> statuses) {
        this.statuses = statuses;
    }

    public DualListModel<Product> getProducts() {
        return products;
    }

    public void setProducts(DualListModel<Product> products) {
        this.products = products;
    }

    public DualListModel<ProductCategory> getCategories() {
        return categories;
    }

    public void setCategories(DualListModel<ProductCategory> categories) {
        this.categories = categories;
    }
    
    public void setClientController(WebClientController clientController) {
        this.clientController = clientController;
        security = clientController.getServerManager().getSecurity();
        requestManager = clientController.getServerManager().getRequestManager();
        productManager = clientController.getServerManager().getProductManager();
        reportManager = clientController.getServerManager().getReportManager();
    }

    private List<Request> getRequests() {
        ReportProperties rp = new ReportProperties();
        rp.setUsers(users.getTarget());
        rp.setManagers(managers.getTarget());
        rp.setProducts(products.getTarget());
        rp.setStatuses(statuses.getTarget());
        if (dateFrom != null){
            rp.setDateFrom(dateFrom);
        }
        if (dateTo != null){
            rp.setDateTo(dateTo);
        }
//        if (description.trim().length()>0){
//            rp.setDescripton(description);
//        }
//        if (message.trim().length()>0){
//            rp.setMessage(message);
//        }
        switch (common){
            case 1: rp.setIsCommon(true); break;
            case 2: rp.setIsCommon(false);break;
        }
        
        return reportManager.getRequests(rp);
    }

    public Date getDateFrom() {
        return dateFrom;
    }

    public void setDateFrom(Date dateFrom) {
        this.dateFrom = dateFrom;
    }

    public Date getDateTo() {
        return dateTo;
    }

    public void setDateTo(Date dateTo) {
        this.dateTo = dateTo;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCommon() {
        return common;
    }

    public void setCommon(int common) {
        this.common = common;
    }
}
